package com.qiya.LiaoDa;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import com.qiya.LiaoDa.Util.ToolbarUtil;
import com.qiya.LiaoDa.ZViewPagerAdapter.ZViewPagerAdapter;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {
    private static final String TAG = "MainActivity";
    private static final int ADD_CLASS_CODE = 1;
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;

    public Toolbar toolbar;
    public ImageButton mFabButton;

    private RadioGroup rg;
    private RadioButton rad_btn_xianzhi;
    private RadioButton rad_btn_xiaoxi;
    private RadioButton rad_btn_wode;
    private RadioButton rad_btn_shouye;
    private ViewPager viewpager;
    private ZViewPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = new ZViewPagerAdapter(getSupportFragmentManager());
        initViews();
        initToolbar();

    }



    private void initViews() {

        rg =  findViewById(R.id.rg);
        rad_btn_xianzhi = findViewById(R.id.rad_btn_xianzhi);
        rad_btn_xiaoxi = findViewById(R.id.rad_btn_xiaoxi);
        rad_btn_wode =  findViewById(R.id.rad_btn_wode);
        rad_btn_shouye =  findViewById(R.id.rad_btn_shouye);


        viewpager = findViewById(R.id.viewpager);

        rg.setOnCheckedChangeListener(this);
        viewpager.setAdapter(mAdapter);
        viewpager.setCurrentItem(0);
        viewpager.addOnPageChangeListener(this);
        rad_btn_shouye.setChecked(true);

    }
    private void  initToolbar() {
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
//        toolbar.setTitle("haha");
//        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
        // 设置返回键
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        getSupportActionBar().setHomeButtonEnabled(true);
        // 实现居中
        ToolbarUtil.setTitleCenter(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.exit) {

            return true;
        } else if (id == R.id.add) {
        }

        return super.onOptionsItemSelected(item);
    }
    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
          toolbar.setTitle(title);

    }

//    /**
//     * 设置标题
//     *
//     * @param
//     */
//    public void setTitle(int title) {
//        toolbar.setTitle(title);
//    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rad_btn_shouye:
                viewpager.setCurrentItem(PAGE_ONE);
                Log.i(TAG, "onPageSelected: 这是首页111");
                break;
            case R.id.rad_btn_xianzhi:
                viewpager.setCurrentItem(PAGE_TWO);
                break;
            case R.id.rad_btn_xiaoxi:
                viewpager.setCurrentItem(PAGE_THREE);
                break;
            case R.id.rad_btn_wode:
                viewpager.setCurrentItem(PAGE_FOUR);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
               setTitle(getString(R.string.shouye));
                Log.i(TAG, "onPageSelected: 这是首页");
                break;
            case 1:
               setTitle(getString(R.string.shangping));
                Log.i(TAG, "onPageSelected: 这是商品");
                break;
            case 2:
               setTitle(getString(R.string.gouwuche));
                Log.i(TAG, "onPageSelected: 这是购物车");
                break;
            case 3:
               setTitle(getString(R.string.wode));
                Log.i(TAG, "onPageSelected: 这是我的");
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {


        Log.i(TAG, "onPageScrollStateChanged: state::" + state);
        if (state == 2) {
            int currentItemPosition = viewpager.getCurrentItem();
            Log.w(TAG, "onPageScrollStateChanged: currentItemPosition::" + currentItemPosition);
            switch (currentItemPosition) {
                case PAGE_ONE:
                    rad_btn_shouye.setChecked(true);
                    break;
                case PAGE_TWO:
                    rad_btn_xianzhi.setChecked(true);
                    break;
                case PAGE_THREE:
                    rad_btn_xiaoxi.setChecked(true);
                    break;
                case PAGE_FOUR:
                    rad_btn_wode.setChecked(true);
                    break;
            }
        }
    }

}
